import router from './router'
import store from './store'
import NProgress from 'nprogress'//引入进度条插件
import 'nprogress/nprogress.css' // 引入进度条css样式文件
import { asyncRoutes } from '@/router'

const WHITE_LIST = ['/login', '/404']
//路由前置守卫(只要是访问页面，就会经过守卫)
router.beforeEach(async (to, from, next) => {
    NProgress.start()

    if (store.getters.token) {
        if (to.path === '/login') {
            next('/')
            // 拦截，手动关闭进度条
            NProgress.done()
        } else {
            // 获取用户信息的方法应该在此处调用
            // store.dispatch('user/getUserInfo').then(res=>{
            //     next()
            // })
            if (!store.state.user.userInfo.userId) {
                const res = await store.dispatch('user/getUserInfo')
                console.log(res, 999);

                const otherRoutes = await store.dispatch('permission/filterRoutes', res.roles.menus)

                // 注意点：动态添加路由是异步的,这里如果这样处理结果是还没有添加完成就进去了，所以要处理一下
                router.addRoutes([
                    ...otherRoutes,
                    { path: '*', redirect: '/404', hidden: true }
                ])
                // 为了避免路由规则的问题，固定语法解决
                next({
                    ...to,
                    replace: true
                })
                return
            }
            next()
        }
    } else {
        if (WHITE_LIST.includes(to.path)) {
            next()
        } else {
            next('/login')
            // 拦截，手动关闭进度条
            NProgress.done()
        }
    }
})

// 路由后置守卫(只要进入到页面中，就会经过后置守卫)
router.afterEach((to, from) => {
    NProgress.done()
})




// import router from './router'
// import store from './store'
// import { Message } from 'element-ui'
// import NProgress from 'nprogress' // progress bar
// import 'nprogress/nprogress.css' // progress bar style
// import { getToken } from '@/utils/auth' // get token from cookie
// import getPageTitle from '@/utils/get-page-title'

// NProgress.configure({ showSpinner: false }) // NProgress Configuration

// const whiteList = ['/login'] // no redirect whitelist

// router.beforeEach(async(to, from, next) => {
//   // start progress bar
//   NProgress.start()

//   // set page title
//   document.title = getPageTitle(to.meta.title)

//   // determine whether the user has logged in
//   const hasToken = getToken()

//   if (hasToken) {
//     if (to.path === '/login') {
//       // if is logged in, redirect to the home page
//       next({ path: '/' })
//       NProgress.done()
//     } else {
//       const hasGetUserInfo = store.getters.name
//       if (hasGetUserInfo) {
//         next()
//       } else {
//         try {
//           // get user info
//           await store.dispatch('user/getInfo')

//           next()
//         } catch (error) {
//           // remove token and go to login page to re-login
//           await store.dispatch('user/resetToken')
//           Message.error(error || 'Has Error')
//           next(`/login?redirect=${to.path}`)
//           NProgress.done()
//         }
//       }
//     }
//   } else {
//     /* has no token*/

//     if (whiteList.indexOf(to.path) !== -1) {
//       // in the free login whitelist, go directly
//       next()
//     } else {
//       // other pages that do not have permission to access are redirected to the login page.
//       next(`/login?redirect=${to.path}`)
//       NProgress.done()
//     }
//   }
// })

// router.afterEach(() => {
//   // finish progress bar
//   NProgress.done()
// })
